/*
* @Author: dengjiayao
* @Date:   2017-08-11 09:51:09
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-08-16 15:27:34
*/
import extend from 'extend'

import Pages from '../../component/pages/pages.vue'

import { showMsg, getExpertList, deleteExpert, exportExpert } from '../../vuex/action'

export default {
  name: 'expert-list',
  components: {
    'vue-pages': Pages
  },
  props: ['mainCategory', 'industryCategory', 'exportPrivilege'],
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        name: '',
        mainCategoryId: '-1',
        industryCategoryId: '-1',
        phone: '',
        email: '',
        company: '',
        experience: ''
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.expertNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getExpertList,
      xDelete: deleteExpert,
      xExport: exportExpert
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getExtParams() {
      let ext = {};
      ['mainCategoryId', 'industryCategoryId'].forEach((key) => {
        let val = this.filter[key] + ''
        if (val > -1) {
          ext[key] = val
        }
      });
      ['name', 'phone', 'email', 'company', 'experience'].forEach((key) => {
        let val = (this.filter[key] + '')
        if (val.length > 0) {
          ext[key] = val
        }
      })
      return ext
    },
    getList({start=this.pages.start, limit=this.pages.limit}) {
      let params = {
        start: start,
        limit: limit
      }
      let extPrams = this.getExtParams()
      params = extend({}, params, extPrams)
      this.xGetList(params)
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalcount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    searchExpert() {
      this.pages.start = 0
      this.getList({})
    },
    exportExpert() {
      let params = this.getExtParams()
      this.xExport(params)
    },
    clearFilterForm() {
      this.filter = {
        name: '',
        mainCategoryId: '-1',
        industryCategoryId: '-1',
        phone: '',
        email: '',
        company: '',
        experience: ''
      }
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
